SlidingWindowRateLimiter
Generic sliding-window rate limiter. Tracks timestamped events per key and reports a key as limited once it accumulates at least maxAttempts events within the trailing window. Empty trackers are evicted on read so memory does not grow without bound for keys that fire a few times and then go quiet.
Constructores
public SlidingWindowRateLimiter(int maxAttempts, TimeSpan? window, IEqualityComparer<TKey> comparer, Func<DateTime> now)
slidingWindowRateLimiter = SlidingWindowRateLimiter(maxAttempts, window, comparer, now)
Initializes a new instance of the SlidingWindowRateLimiter class.
- maxAttempts
- Maximum events allowed within the window before a key is limited.
- window
- Trailing time window. Defaults to one minute.
- comparer
- Optional key comparer.
- now
- Optional clock (UTC). Defaults to UtcNow; injectable for tests.
Propiedades
public int TrackedCount { get; }
value = slidingWindowRateLimiter.TrackedCount
Number of keys currently tracked. Exposed for diagnostics / tests to verify that empty trackers are evicted and do not accumulate.
Métodos
public bool IsLimited(TKey key)
result = slidingWindowRateLimiter.IsLimited(key)
Whether the key has reached the limit within the trailing window. Prunes expired events first and evicts the tracker entirely when nothing recent remains.
public void Record(TKey key)
slidingWindowRateLimiter.Record(key)
Records one event (e.g. a failed attempt) for the key.
public void Reset(TKey key)
slidingWindowRateLimiter.Reset(key)
Clears all tracked events for the key (e.g. after a success).